package net.zjitc.tree.linked;

public class MyTree<E> implements BinaryTree<E> {


    private TreeNode<E> root;

    public MyTree(TreeNode<E> root) {
        this.root = root;
    }

    public void setRoot(TreeNode<E> root) {
        this.root = root;
    }

    public TreeNode<E> getRoot() {
        return root;
    }

    @Override
    public void insertL(TreeNode<E> node, E var) {
        if (node.left != null) {
            throw new RuntimeException("左子树已经存在，不能添加");
        }
        TreeNode<E> newNode = new TreeNode<>(var);
        node.left = newNode;
    }

    @Override
    public void insertR(TreeNode<E> node, E var) {
        if (node.right != null) {
            throw new RuntimeException("右子树已经存在，不能添加");
        }
        TreeNode<E> newNode = new TreeNode<>(var);
        node.right = newNode;
    }

    @Override
    public TreeNode<E> getLChild(TreeNode<E> node) {
        return node.left;
    }

    @Override
    public TreeNode<E> getRCHild(TreeNode<E> node) {
        return node.right;
    }

    public void prev(TreeNode node){
        if (node!=null){
            //根
            System.out.println(node.data);
            //左
            prev(node.left);
            //右
            prev(node.right);
        }
    }
    public void mid(TreeNode node){
        if (node!=null){

            //左
            mid(node.left);
            //根
            System.out.println(node.data);
            //右
            mid(node.right);
        }
    }
    public void after(TreeNode node){
        if (node!=null){

            //左
            after(node.left);
            //右
            after(node.right);
            //根
            System.out.println(node.data);
        }
    }
    public void traversal(int type){
        switch (type){
            case 1:
                prev(root);
                break;
            case 2:
                mid(root);
                break;
            case 3:
                after(root);
                break;
        }
    }
}
